﻿using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCS.UI.Utils;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCS.UI.ChildWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using System;

namespace Baosight.ZNH.ZMCS.UI
{
    /// <summary>
    /// 工程查询
    /// </summary>
    /// Title:ZMCS20,ServiceZMCS20
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 张小飞
    /// Version: 1.0
    /// History:
    ///       2011-06-01  张小飞   [创建]
    ///
    public partial class ZMCS51 : EFViewCmes
    {
        private EngineeringBlock _block = new EngineeringBlock();

        public ZMCS51()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(EIInfo initData)
        {
            this.comboBoxItem.Value = initData.Get("comboBoxItem");
            this.value.Value = initData.Get("value");
            this.tableName.Value = initData.Get("tableName");

            this.Loaded += new RoutedEventHandler(ZMCS51_Loaded);
            initData.AddBlock(_block.GetKeyProject(true));//是否重点工程下拉
            initData.AddBlock(_block.GetEngineeringStatus(true));//工程状态下拉
            initData.AddBlock(_block.GetEngineeringType(true));//工程类型
            base.Initialize(initData);
        }

        /// <summary>
        /// 当选中单位，类别，专项资金时，为查询区的对应单位，类别，专项资金赋初值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZMCS51_Loaded(object sender, RoutedEventArgs e)
        {
            string _value = this.value.Value == null ? "" : this.value.Value.ToString().Trim();

            if ("$unit" == this.comboBoxItem.GetDataValue().ToString() && "topUnit" != this.value.GetDataValue().ToString())
            {
                this.fdType.Value = "";//工程类别
                this.fdCapitalSource.Value = "";//专项资金
            }
            else if ("$category" == this.comboBoxItem.GetDataValue().ToString())
            {
                this.fdType.Value = "";
                if (!_value.Equals("topCategory"))
                {
                    string _tableName = this.tableName.Value == null ? "" : this.tableName.Value.ToString().Trim();
                    if (!"category".Equals(_tableName))
                    {
                        _value = _tableName;
                    }
                    switch (_value)
                    {
                        case "CM":
                            this.fdType.Value = "0";
                            break;
                        case "JJ":
                            this.fdType.Value = "1";
                            break;
                        case "TF":
                            this.fdType.Value = "2";
                            break;
                        case "QT":
                            this.fdType.Value = "3";
                            break;
                        default:
                            break;
                    }
                }
                this.fdType.IsEnabled = false;
                this.fdCapitalSource.Value = "";//专项资金
            }
            else if ("$specialFund" == this.comboBoxItem.GetDataValue().ToString())
            {
                this.fdCapitalSource.Value = _value.Equals("topSpecialFund") ? "" : this.value.Value.ToString();//为查询区专项资金赋初值
                this.fdCapitalSource.IsEnabled = false;
                this.fdType.Value = "";//工程类别
            }
            else
            {
                this.fdType.Value = "";//工程类别
                this.fdCapitalSource.Value = "";//专项资金
            }

        }

        /// <summary>
        /// 列表中点击查看执行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            string _type = row["fdType"] == null ? "" : row["fdType"].ToString();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "工程查询-查看", "detail", _type, sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.specialFund"));//专项资金
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.engineeringGroup"));//工程组
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.constructionUnit"));//部门单位
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.engineeringParentId"));//工程所属
            _eIInfo.AddBlock(_block.GetKeyProject(false));//重点工程
            _eIInfo.AddBlock(_block.GetEngineeringStatus(false));//工程状态
            string parentTypes = _eIInfo.GetBlock("result").Get(0, "fdType") == null ? "" : _eIInfo.GetBlock("result").Get(0, "fdType").ToString().Trim();//大类

            if ("0".Equals(parentTypes))
            {
                //采煤
                _eIInfo.AddBlock(_block.GetEngineeringClassCM(false));//采煤分类
            }
            else if ("1".Equals(parentTypes))
            {
                //掘进
                _eIInfo.AddBlock(_block.GetEngineeringClassJJ(false));//掘进分类
            }
            else if ("2".Equals(parentTypes))
            {
                //通防
                _eIInfo.AddBlock(_block.GetEngineeringClassTF(false));//通防分类
            }
            else
            {
                //其他
                _eIInfo.AddBlock(_block.GetEngineeringClassQT(false));//其他分类
            }

            Engineering _eng = new Engineering(_dialogType);//构造一个新的修改页面
            _eng.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eng.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = _eng;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 弹出窗口关闭后执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }
    }
}
